/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import pojo.Khachhang;
import pojo.Nhanvien;
import util.HibernateUtil;

/**
 *
 * @author Cong
 */
public class daoKhachHang {
    
    public static List<Khachhang> LaydsKhachHang() {
        List ds = null;
        String sql = "from Khachhang order by id asc";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static List<Nhanvien> GetNV(Nhanvien nv) {
        List<Nhanvien> ds = null;
        String sql = "from Nhanvien where hoten=:hoten";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            qr.setParameter("hoten",nv.getHoten());
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static boolean ThemKhachHang(Khachhang kh) {
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        boolean kq = true;
        try {
            tS = sS.beginTransaction();
            sS.save(kh);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            kq = false;
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static Khachhang GetKH(Khachhang kh) {
        Khachhang kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            kq = (Khachhang)sS.get(Khachhang.class, kh.getId());
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean XoaKH(Khachhang kh) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Khachhang temp = GetKH(kh);
        if (temp == null) {
            return false;
        }
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.delete(temp);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean LuuKH(Khachhang kh) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Khachhang temp = GetKH(kh);
        if (temp == null) {
            return false;
        }
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.update(kh);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static List<Khachhang> FindKH(Khachhang kh) {
        List<Khachhang> kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria cR = sS.createCriteria(Khachhang.class);
            cR.add(Restrictions.ilike("tenkhachhang","%" + kh.getTenkhachhang() + "%"));
            cR.add(Restrictions.ilike("dienthoai","%" + kh.getDienthoai() + "%"));
            cR.add(Restrictions.ilike("email","%" + kh.getEmail() + "%"));
            cR.add(Restrictions.ilike("diachi","%" + kh.getDiachi() + "%"));
            cR.add(Restrictions.ilike("gioitinh","%" + kh.getGioitinh() + "%"));
            if (kh.getNgaysinh() != null) {
                cR.add(Restrictions.eq("ngaysinh",kh.getNgaysinh()));
            }
            if (kh.getNgaytaotaikhoan() != null) {
                cR.add(Restrictions.eq("ngaytaotaikhoan",kh.getNgaytaotaikhoan()));
            }
            if (kh.getNhanvien() != null) {
                cR.add(Restrictions.eq("nhanvien",kh.getNhanvien()));
            }
            kq = cR.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
}
